<?php

session_start();

if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {

    header("Location: login.php");
}
//
$_SESSION['DataType'] = "";
$_SESSION['Trace'] = "";
//
include 'i_functions.php';
include 'i_init_client.php';
//
$db_conn = db_connect();
$db_handle = $db_conn['db_handle'];
$db_found = $db_conn['db_found'];

if ($db_found) {
    include 'i_head.php';
    $HTML = "<p>exporting claims data to csv file....";
    print $HTML;
    include 'i_foot.html';
    //exec('pwd;ls', $tmp1, $retval);
    //print '<br>';
    //var_dump($tmp1);
    //print '<br>' . $retval;
    exec('rm /tmp/ClaimsStatus.txt', $tmp2, $retval);
    //print '<br>';
    //var_dump($tmp2);
    //print '<br>' . $retval;
    $SQL = "SELECT Status, Date(StatusDate) AS Date, CONCAT(client.Title, ' ', client.Surname) AS Client, LenderName 
            FROM PPI.claims,client 
            WHERE ClientID = client.ID 
            ORDER BY Status
            into outfile '/tmp/ClaimsStatus.txt'
            fields terminated by '\t'
            lines terminated by '\n'";
    //print '<br>SQL=' . $SQL;
    $result = mysql_query($SQL);
    //print '<br>result from SQL ' . $result;
    exec('./scripts/SendClaimsStatus', $tmp3, $retval);
    //print '<br>';
    //var_dump($tmp3);
    print '<br>... done!';
}
?>